package com.xiaoq.sites.repository.impl;

import java.util.ArrayList;
import java.util.List;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.sites.entity.BannerEntity;
import com.xiaoq.sites.repository.IBannerRepository;

@Repository
public class BannerRepositoryImpl implements IBannerRepository {
	
	@Autowired
	private Datastore datastore;

	@Autowired
	private IdGenerator idGenerator;

	private static final Logger LOG = LoggerFactory.getLogger(BannerRepositoryImpl.class);
	
	@Override
	public List<BannerEntity> findBannerList(String channelCode,String cityCode) {
		Query<BannerEntity> findBannerQuery = datastore.find(BannerEntity.class);
		List<BannerEntity> bannerList = new ArrayList<>();
		findBannerQuery.field("zoneCity").equal(cityCode);
		if(findBannerQuery.count()==0){
			findBannerQuery = datastore.find(BannerEntity.class);
		}
		if("2000".equals(channelCode)){
			findBannerQuery = datastore.find(BannerEntity.class);
		}
		if (StringUtils.isNotEmpty(channelCode)) {
			findBannerQuery.field("channelCode").equal(channelCode).order("-createdAt");
			bannerList = findBannerQuery.asList();

		} else {
			LOG.error("channelCode值传递不能为空！");
		}
		List<BannerEntity> bannerResultList = new ArrayList<>();
		if(bannerList!=null&&bannerList.size()!=0){
			if("2000".equals(channelCode)){
				bannerResultList.add(bannerList.get(0));
			}else{
				int num = 0;
				if(bannerList.size()>5){
					num =5;
				}else{
					num = bannerList.size();
				}
				for(int i=0;i<num;i++){
					bannerResultList.add(i,bannerList.get(i));
				}
			}
		}
		return bannerResultList;
	}

	@Override
	public boolean addBanner(BannerEntity bannerEntity) {
		if(bannerEntity!=null){
			bannerEntity.setId(idGenerator.nextId());
			bannerEntity.setCreatedAt(System.currentTimeMillis());
			datastore.save(bannerEntity);
			return true;
		}
		return false;
	}

	@Override
	public boolean delBanner(String bannerId) {
		if(StringUtils.isNotEmpty(bannerId)){
			datastore.delete(datastore.createQuery(BannerEntity.class).field("_id").equal(bannerId));
			return true;
		}

		return false;
	}

	@Override
	public boolean updateBanner(BannerEntity bannerEntity) {
		String bannerId  = bannerEntity.getId();
		Query<BannerEntity> bannerQuery = datastore.createQuery(BannerEntity.class).field("_id").equal(bannerId);
		UpdateOperations<BannerEntity> bannerUpdate = datastore.createUpdateOperations(BannerEntity.class);
		if(bannerEntity.getChannelCode()!=null)
			bannerUpdate = bannerUpdate.set("channelCode",bannerEntity.getChannelCode());
		if(bannerEntity.getChannelName()!=null)
			bannerUpdate = bannerUpdate.set("channelName",bannerEntity.getChannelName());
		if(bannerEntity.getSortNum()!=null)
			bannerUpdate = bannerUpdate.set("sortNum",bannerEntity.getSortNum());
		if(bannerEntity.getTargetType()!=null)
			bannerUpdate = bannerUpdate.set("targetType",bannerEntity.getTargetType());
		if(bannerEntity.getTargetId()!=null)
			bannerUpdate = bannerUpdate.set("targetId",bannerEntity.getTargetId());
		if(bannerEntity.getLinkParams()!=null)
			bannerUpdate = bannerUpdate.set("linkParams",bannerEntity.getLinkParams());
		if(bannerEntity.getTitle()!=null)
			bannerUpdate = bannerUpdate.set("title",bannerEntity.getTitle());
		if(bannerEntity.getDescriptions()!=null)
			bannerUpdate = bannerUpdate.set("descriptions",bannerEntity.getDescriptions());
		if(bannerEntity.getMainImgUrl()!=null)
			bannerUpdate = bannerUpdate.set("mainImgUrl",bannerEntity.getMainImgUrl());
		if(bannerEntity.getStaticUrl()!=null)
			bannerUpdate = bannerUpdate.set("staticUrl",bannerEntity.getStaticUrl());
		if(bannerEntity.getMerchantId()!=null)
			bannerUpdate = bannerUpdate.set("merchantId",bannerEntity.getMerchantId());
		if(bannerEntity.getShopId()!=null)
			bannerUpdate = bannerUpdate.set("shopId",bannerEntity.getShopId());
		if(bannerEntity.getShopType()!=null)
			bannerUpdate = bannerUpdate.set("shopType",bannerEntity.getShopType());
		if(bannerEntity.getZoneProvince()!=null)
			bannerUpdate = bannerUpdate.set("zoneProvince",bannerEntity.getZoneProvince());
		if(bannerEntity.getZoneCity()!=null)
			bannerUpdate = bannerUpdate.set("zoneCity",bannerEntity.getZoneCity());
		if(bannerEntity.getZoneDistrict()!=null)
			bannerUpdate = bannerUpdate.set("zoneDistrict",bannerEntity.getZoneDistrict());
		if(bannerEntity.getMemo()!=null)
			bannerUpdate = bannerUpdate.set("memo",bannerEntity.getMemo());
		datastore.update(bannerQuery, bannerUpdate);
		return true;
	}

	@Override
	public BannerEntity findByBannerId(String bannerId) {
		return datastore.get(BannerEntity.class, bannerId);
	}

	@Override
	public List<BannerEntity> findBannerList(PagingVo pvo, String cityCode) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		List<BannerEntity> bannerList = new ArrayList<>();
		Query<BannerEntity> bannerQuery = datastore.find(BannerEntity.class).field("zoneCity").equal(cityCode);
		total = bannerQuery.count();
		bannerList = bannerQuery.offset(pn).limit(ps).asList();
		pvo.setTc(total);
		return bannerList;
	}

	@Override
	public List<BannerEntity> findBannerByChannelCodeAndCityCodeList(PagingVo pagingVo,String channelCode, String cityCode) {
		Query<BannerEntity> findBannerQuery = datastore.find(BannerEntity.class);
		List<BannerEntity> bannerList = new ArrayList<>();
		findBannerQuery.field("zoneCity").equal(cityCode);
		if (StringUtils.isNotEmpty(channelCode)) {
			findBannerQuery.field("channelCode").equal(channelCode);
		}
		findBannerQuery.order("sortNum");
		if (pagingVo != null){
			findBannerQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
		}
		return findBannerQuery.asList();
	}
}
